package com.linxz.video.mine;

import android.content.Context;
import android.media.MediaPlayer;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.linxz.video.R;

/**
 * @author Linxz
 * 创建日期：2019年10月15日 09:42
 * version：v4.5.4
 * 描述：
 */
public class BaseVideoView extends FrameLayout {

    private MediaPlayer mediaPlayer;
    private SurfaceHolder surfaceHolder;
    private Button btnPlay;
    private Button btnPause;
    private Button btnStop;
    private VideoStatus mVideoStatus=VideoStatus.PREPARE;
    private String sourcePath;

    enum VideoStatus{
        PREPARE,
        PLAYING,
        START,
        PAUSE,
        STOP,
        COMPLETE,
        DESTROY
    }

    public BaseVideoView(@NonNull Context context) {
        super(context);
    }

    public BaseVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public BaseVideoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        setListener();
    }



    private void init(){
        mediaPlayer=new MediaPlayer();
        View contentView= LayoutInflater.from(getContext()).inflate(R.layout.lib_video_base_video,this);
        btnPlay=contentView.findViewById(R.id.btnPlay);
        btnPause=contentView.findViewById(R.id.btnPause);
        btnStop=contentView.findViewById(R.id.btnStop);
        SurfaceView surfaceView=contentView.findViewById(R.id.surfaceView);
        surfaceHolder=surfaceView.getHolder();
    }

    private void setListener(){
        btnPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mVideoStatus != VideoStatus.PLAYING){
                    play();
                }
            }
        });
        btnPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                pause();
            }
        });
        btnStop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                stop();
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                complete();
            }
        });
    }


    private void play(){
       try {
           if (TextUtils.isEmpty(sourcePath)){
               Toast.makeText(getContext(),"sourcePath不能为空",Toast.LENGTH_SHORT).show();
               return;
           }
           mediaPlayer.reset();
           mediaPlayer.setDataSource(sourcePath);
           mediaPlayer.setDisplay(surfaceHolder);
           mediaPlayer.prepare();
           mediaPlayer.start();
           mVideoStatus = VideoStatus.START;
           mVideoStatus = VideoStatus.PLAYING;
       }catch (Exception e){
           e.printStackTrace();
       }
    }

    private void pause(){
        if (mVideoStatus == VideoStatus.PLAYING){
            mediaPlayer.pause();
            mVideoStatus = VideoStatus.PAUSE;
        }
    }

    private void stop(){
        if (mVideoStatus == VideoStatus.PLAYING){
            mediaPlayer.stop();
            mVideoStatus = VideoStatus.STOP;
        }
    }

    private void complete(){
        mVideoStatus = VideoStatus.COMPLETE;
        Toast.makeText(getContext(),"播放完毕",Toast.LENGTH_SHORT).show();
    }

    private void destory(){
        stop();
        mediaPlayer.release();
        mVideoStatus = VideoStatus.DESTROY;
    }

    public void setDataSource(String sourcePath){
        this.sourcePath=sourcePath;
    }

}
